//
//  ReadAddViewController.m
//  WYnewsss
//
//  Created by lanou on 16/4/25.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ReadAddViewController.h"
#import "CycleScrollView.h"
#import "NetWorkrequestManager.h"
#import "ReadAddModel.h"
#import "ReadAddInfoViewController.h"
#import <UIImageView+WebCache.h>
#import "BaseTableViewCell.h"
#import "FactoryTableViewCell.h"
#import "JRSegmentViewController.h"
#import "MyNavigationViewController.h"
#import "ReadListViewController.h"
#import "ReadMyListViewController.h"
#import "ReadAddViewCell.h"
#import "ReadMyLastDetailViewcell.h"

#define NavigationBarHeight     44.0   //导航条高度
@interface ReadAddViewController ()<UITableViewDataSource,UITableViewDelegate>
   @property(nonatomic,strong)NSMutableArray *addArray;
@property(nonatomic,strong)NSMutableArray *bannerArray;
@property(nonatomic,strong)CycleScrollView *cycleScrollview;

@property (weak, nonatomic) IBOutlet UITableView *reAddTableView;

@end

@implementation ReadAddViewController

-(NSMutableArray *)bannerArray{
    if (!_bannerArray) {
        self.bannerArray=[NSMutableArray array];
    }
    return _bannerArray;
}

-(NSMutableArray *)addArray{
    if (!_addArray) {
        self.addArray =[NSMutableArray array];
    }
    return _addArray;
}
-(void)requestData{
    [NetWorkRequestManager requestWithType:GET urlString:@"http://c.m.163.com/nc/topicset/subscribe/recommend.html" parDic:nil finish:^(NSData *data) {
        NSDictionary *dataDic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        //NSLog(@"%@",dataDic);
        NSArray * bunnerArray =dataDic[@"bannerlist"];
        for (NSDictionary *bunnerDic in bunnerArray) {
            ReadAddModel *bunnerModel =[[ReadAddModel alloc]init];
            [bunnerModel setValuesForKeysWithDictionary:bunnerDic];
            [self.bannerArray addObject:bunnerModel];
        }
        NSArray *addArray = dataDic[@"recommendlist"];
        for (NSDictionary *addDic in addArray) {
            ReadAddModel *addModel =[[ReadAddModel alloc]init];
            [addModel setValuesForKeysWithDictionary:addDic];
            [self.addArray addObject:addModel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self creatCycleScrollView];
            [_reAddTableView reloadData];
        });

    } error:^(NSError *error) {
        
    }];
}

//-(void)requestData{
//    [NetWorkrequestManage requestWithType:GET url:@"http://c.m.163.com/nc/topicset/subscribe/recommend.html" parameters:nil finish:^(NSData *data) {
//        NSDictionary *dataDic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        //NSLog(@"%@",dataDic);
//        NSArray * bunnerArray =dataDic[@"bannerlist"];
//        for (NSDictionary *bunnerDic in bunnerArray) {
//            ReadAddModel *bunnerModel =[[ReadAddModel alloc]init];
//            [bunnerModel setValuesForKeysWithDictionary:bunnerDic];
//            [self.bannerArray addObject:bunnerModel];
//        }
//        NSArray *addArray = dataDic[@"recommendlist"];
//        for (NSDictionary *addDic in addArray) {
//            ReadAddModel *addModel =[[ReadAddModel alloc]init];
//            [addModel setValuesForKeysWithDictionary:addDic];
//            [self.addArray addObject:addModel];
//        }
//        dispatch_async(dispatch_get_main_queue(), ^{
//            [self creatCycleScrollView];
//            [_reAddTableView reloadData];
//        });
//    } error:^(NSError *error) {
//        
//    }];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
  self.automaticallyAdjustsScrollViewInsets = NO;
    
//    UINavigationBar *bar =[[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
//    UINavigationItem *item =[[UINavigationItem alloc]initWithTitle:@"今日订阅推荐"];
//    [bar pushNavigationItem:item animated:YES];
//    UIBarButtonItem *leftButon =[[UIBarButtonItem alloc]initWithTitle:@"< 返回" style:(UIBarButtonItemStyleDone) target:self action:@selector(back)];
//    self.leftButton.backgroundColor =[UIColor blackColor];
//    [item setLeftBarButtonItem:leftButon];
//    [self.view addSubview:bar];
    self.navigationItem.title=@"今日订阅推荐";
   
    [self requestData];
    [_reAddTableView registerNib:[UINib nibWithNibName:@"ReadAddViewCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([ReadAddModel class])];
   
     // Do any additional setup after loading the view from its nib.
}

//-(void)back{
//    [self dismissViewControllerAnimated:YES completion:nil];
//}

- (void)creatCycleScrollView {

    _cycleScrollview = [[CycleScrollView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, 160) animationDuration:2];

    NSMutableArray *viewsArray = [@[] mutableCopy];
    
    for (int i = 0; i < _bannerArray.count; ++i) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:_cycleScrollview.bounds];
        imageView.backgroundColor = [UIColor colorWithRed:arc4random()%256 / 255.f green:arc4random()%256 / 255.f blue:arc4random()%256 / 255.f alpha:1];
        
        ReadAddModel *model = _bannerArray[i];
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.imgsrc]];
        [viewsArray addObject:imageView];
    }
    [self.view addSubview:_cycleScrollview];
    
    _cycleScrollview.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex){
        return viewsArray[pageIndex];
    };
    _cycleScrollview.totalPagesCount = viewsArray.count;
    
    __weak NSArray *cycelArray = _bannerArray;
    __weak ReadAddViewController *readAddVC = self;
    _cycleScrollview.TapActionBlock = ^(NSInteger pageIndex){
         NSLog(@"点击了第%ld个",(long)pageIndex);
        
        ReadAddModel *addModel = [cycelArray objectAtIndex:pageIndex];
        
        NSLog(@"url = %@", addModel.tid);
        
        NSArray *cArray = [addModel.tid componentsSeparatedByString:@"/"];
        ReadAddInfoViewController *addInfoVC = [[ReadAddInfoViewController alloc] init];
        addInfoVC.tid = [cArray lastObject];
        [readAddVC.navigationController pushViewController:addInfoVC animated:YES];
    };
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _addArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 165;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    ReadAddModel *model =_addArray[indexPath.row];
    BaseTableViewCell *cell =[FactoryTableViewCell creatTableViewCell:model withTableView:tableView andIndexPath:indexPath];
    [cell setDataWithModel:model];
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ReadAddModel *model =_addArray[indexPath.row];
    ReadAddInfoViewController *infoVC =[[ReadAddInfoViewController alloc]init];
    infoVC.tid=model.tid;
    [self.navigationController pushViewController:infoVC animated:YES];
   
}



//- (void)setupChildViewController:(UIViewController *)viewController title:(NSString *)title image:(NSString *)image selectImage:(NSString *)selectImage {
//    
//    MyNavigationViewController *navc = [[MyNavigationViewController alloc] initWithRootViewController:viewController];
//    
//    if ([NSStringFromClass([viewController class]) isEqualToString:@"JRSegmentViewController"]) {
//        ReadListViewController *readVC = [[ReadListViewController alloc] init];
//        
//        ReadMyListViewController *myListVC =[[ReadMyListViewController alloc]init];
//        
//        JRSegmentViewController *vc = (JRSegmentViewController *)viewController ;
//        vc.segmentBgColor = [UIColor colorWithRed:15.0f/255 green:50.0f/255 blue:110.0f/255 alpha:1.0f];
//        
//        vc.titleColor = [UIColor whiteColor];
//        
//        [vc setViewControllers:@[readVC, myListVC]];
//        
//        [vc setTitles:@[@"推荐阅读", @"我的订阅"]];
//       
//    }
//    //viewController.view.backgroundColor = ArcrandomColor;
//    
//    [self addChildViewController:navc];
//    }








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
